package it.kion.util.ui.ulog2.views;

import java.text.*;
import java.util.Date;

import org.apache.log4j.Level;
import org.apache.log4j.spi.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.*;

class ViewLabelProvider
   extends LabelProvider
   implements ITableLabelProvider
{
   private static final DateFormat DATE_FORMATTER =
      new SimpleDateFormat("HH:mm:ss.SSS");

   public String getColumnText(Object obj, int index)
   {
      LoggingEvent event = (LoggingEvent) obj;
      LocationInfo locationInfo;
      switch (index)
      {
         case Log4jView.COL_TIME :
            return DATE_FORMATTER.format(new Date(event.timeStamp));
         case Log4jView.COL_LEVEL :
            return event.getLevel().toString();
         case Log4jView.COL_MESSAGE :
            return event.getRenderedMessage();
         case Log4jView.COL_CATEGORY :
            return event.getLoggerName();
         case Log4jView.COL_METHOD :
            locationInfo = event.getLocationInformation();
            if (locationInfo != null
               && locationInfo.getMethodName() != null)
            {
               return locationInfo.getMethodName();
            }
            break;
      }
      return "";
   }

   public Image getColumnImage(Object obj, int index)
   {
      if (index == Log4jView.COL_IMAGE)
         return getImage(obj);
      else
         return null;
   }
   public Image getImage(Object obj)
   {
      LoggingEvent event = (LoggingEvent) obj;
      Level level = event.getLevel();

      ISharedImages sharedImages =
         PlatformUI.getWorkbench().getSharedImages();

      return sharedImages.getImage(
         (level == Level.ERROR
            ? ISharedImages.IMG_OBJS_ERROR_TSK
            : (level == Level.WARN
               ? ISharedImages.IMG_OBJS_WARN_TSK
               : null)));
   }
}